package com.example.meituan.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.drawable.Drawable;
import android.service.autofill.UserData;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.example.meituan.R;
import com.example.meituan.until.FileImgUntil;

/*
链接数据库
* */
public class DBUntil extends SQLiteOpenHelper {
   private static final int version=33;//版本号

    private static final String datebaseName="boyue.db";//数据库名称

    private Context context;

    public static SQLiteDatabase con;//链接数据库的连接
    public DBUntil(@Nullable Context context) {
        super(context, datebaseName, null, version,null);
        this.context=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据库
        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_business");//如果这表存在则删
        db.execSQL("create table d_business(s_id varchar(20) primary key," +
                "s_pwd varchar(20)," +
                "s_name varchar(20)," +
                "s_describe varchar(200)," +
                "s_type varchar(20)," +
                "s_img varchar(255))");


        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_user");//如果这表存在则删
        db.execSQL("create table d_user(s_id varchar(20) primary key," +
                "s_pwd varchar(20)," +
                "s_name varchar(20)," +
                "s_phone varchar(200)," +
                "s_shen varchar(200),"+
                "s_img varchar(255))");


        //写一个存储商品的表
        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_shop");//如果这表存在则删
        db.execSQL("create table d_shop(s_id varchar(20) primary key," +
                "s_business_id varchar(20)," +
                "s_name varchar(20)," +
                "s_des varchar(200)," +
                "s_price varchar(200),"+
                 "s_time INTEGER, " +
                "s_type varchar(200))");



        //订单编号 订单时间 购买人id 商品id 收货地址  商家id
        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_orders");//如果这表存在则删
        db.execSQL("create table d_orders(s_id varchar(20) primary key," +
                "s_order_time varchar(200),"+
                "s_business_id varchar(20)," +
                "s_user_id varchar(20),"+
                "s_order_sta varchar(20),"+
                "s_order_address varchar(200)," +
                "s_order_detials_id varchar(200))");



        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_order_detials_id");//如果这表存在则删
        db.execSQL("create table d_order_detials_id(s_id varchar(20) primary key," +
                "s_shop_id varchar(20)," +
                "s_name varchar(20)," +
                "s_des varchar(200)," +
                "s_price varchar(200),"+
                "s_num varchar(200),"+
                "s_send_id varchar(200),"+
                "s_detial_type varchar(200),"+
                "s_img varchar(255))");

        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_type");//如果这表存在则删
        db.execSQL("create table d_type(s_id varchar(20) primary key," +
                "s_shop_id varchar(20)," +
                "s_type_des varchar(200))");


        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_comment");//如果这表存在则删
        db.execSQL("create table d_comment(s_id varchar(20) primary key," +
                "s_user_id varchar(20)," +
                "s_type_id varchar(20)," +
                "s_shop_id varchar(20)," +
                "s_comment varchar(200)," +
                "s_img varchar(255))");

        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_talk");//如果这表存在则删
        db.execSQL("create table d_talk(s_id varchar(20) primary key," +
                "s_user_id varchar(20)," +
                "s_bussiness_id varchar(20)," +
                "s_time INTEGER," +
                "s_if_userId INTEGER," +
                "s_talk varchar(200))");

        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_img");//如果这表存在则删
        db.execSQL("create table d_img(s_id varchar(20) primary key," +
                "s_shop_id varchar(20)," +
                "s_img varchar(255))");

        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_shop_car");//如果这表存在则删
        db.execSQL("create table d_shop_car(s_id varchar(20) primary key," +
                "s_user_id varchar(20)," +
                "s_shop_id varchar(20)," +
                "s_type_id varchar(20)," +
                "s_time INTEGER)" );

        db.execSQL("PRAGMA foreign_keys = false");
        db.execSQL("drop table if exists d_user_address");//如果这表存在则删
        db.execSQL("create table d_user_address(s_id varchar(20) primary key," +
                "s_user_id varchar(20)," +
                "s_name varchar(20)," +
                "s_phone varchar(200)," +
                "s_address varchar(200))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onCreate(db);
    }

}
